package xyz.wjwnya.ask_for_leave_manage_module.serivce;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
import xyz.wjwnya.common.exception.ResponsiveException;

import java.util.List;

/**
 * 请假管理服务，提供请假相关逻辑接口
 * @author wjw
 */
public interface AskForLeaveManageService {

    /**
     * 获取所有请假记录数据
     * @return 请假记录列表
     */
    @NonNull
    List<AskForLeaveRecordData> getAllAskForLeaveRecord();

    /**
     * 获取特定学生的所有请假记录
     * @param studentAccount 学生账号
     * @return 请假记录列表
     */
    @NonNull
    List<AskForLeaveRecordData> getStudentAskForLeaveRecord(String studentAccount);

    /**
     * 学生申请请假
     * @param studentAccount 学生账号
     * @param reason 请假理由
     */
    void askForLeave(@NonNull String studentAccount, @NonNull String reason);

    /**
     * 同意请假
     * @param askId 请假记录id
     * @param studentAccount 学生账号
     * @throws AskNotFoundException 请假记录不存在异常
     * @throws AskHandledException 请假已被处理异常
     */
    void receiveAsk(int askId, @NonNull String studentAccount, @NonNull String handlerAccount);

    /**
     * 请假不通过
     * @param askId 请假id
     * @param studentAccount 学生账号
     * @param reason 不通过理由
     * @throws AskNotFoundException 请假记录不存在异常
     * @throws AskHandledException 请假已被处理异常
     */
    void rejectAsk(int askId, @NonNull String studentAccount, @NonNull String handlerAccount, @NonNull String reason);

    enum AskState {
        WAIT, RECEIVED, REJECT
    }

    @Getter
    @AllArgsConstructor
    class AskForLeaveRecordData {
        private final int id;
        private final String askAccount;
        private final String askUsername;
        private final int askStatus;
        private final long askTime;
        private final String handlerAccount;
        private final String handlerUsername;
        private final String rejectReason;
        private final long handleTime;
        private final String askReason;
    }

    /**
     * 当请假已经审批通过或者不通过时，仍进行这两项操作，则抛出该异常
     */
    class AskHandledException extends ResponsiveException {

        public AskHandledException(int askId) {
            super(HttpStatus.BAD_REQUEST, String.format("this ask for %s is handled", askId));
        }

    }

    /**
     * 当请假不存在时抛出该异常
     */
    class AskNotFoundException extends ResponsiveException {

        public AskNotFoundException(int askId) {
            super(HttpStatus.NOT_FOUND, String.format("this ask for %s is not existed", askId));
        }
    }
}
